package com.bupt.datacollect.controller;


import com.bupt.datacollect.data.DataResponse;
import com.bupt.datacollect.data.Mapping;
import com.bupt.datacollect.data.Packet;
import com.bupt.datacollect.dto.Energy;
import com.bupt.datacollect.dto.PacketDTO;
import com.bupt.datacollect.mapper.PacketListMapper;
import com.bupt.datacollect.service.PacketListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class PacketListController {
    @Autowired
    private PacketListService packetListService;
    @Autowired
    private PacketListMapper packetListMapper;

    @RequestMapping("packetList")
    public DataResponse<PacketDTO> list(HttpServletRequest request,
                                        @RequestParam(value="pageNo") Integer pageNo,
                                        @RequestParam(value="rows") Integer rows){
        DataResponse<PacketDTO> dateResponse = new DataResponse();
        try {
            int page = pageNo<= 0 ? 1:pageNo;
            //每页显示数量
            int limit = rows< 0 ? 20 : rows;
            //从哪条记录开始查
            int start = limit * (page - 1);
            start = start < 0 ? 0 : start;
            //总记录数
            int totalRecords = packetListService.getTotalRecords(request);//this.getTotalRecords(request,search);
            //总页数
            int totalPages = totalRecords / limit;
            if (totalRecords % limit != 0) {
                totalPages++;
            }
            List<PacketDTO> contents = packetListService.list(request,start,limit);

            //当前显示页码
            int currPage = Math.min(totalPages, page);
            dateResponse.setTotalRecords(totalRecords);
            dateResponse.setTotalPages(totalPages);
            dateResponse.setPageNo(currPage);
            dateResponse.setContents(contents);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateResponse;
    }

    /*每个能量计最新的数据*/
    @RequestMapping("lastestList")
    public DataResponse<PacketDTO> lastestList(HttpServletRequest request,
                                        @RequestParam(value="pageNo") Integer pageNo,
                                        @RequestParam(value="rows") Integer rows){
        DataResponse<PacketDTO> dateResponse = new DataResponse();
        try {
            int page = pageNo<= 0 ? 1:pageNo;
            //每页显示数量
            int limit = rows< 0 ? 20 : rows;
            //从哪条记录开始查
            int start = limit * (page - 1);
            start = start < 0 ? 0 : start;
            //总记录数
            int totalRecords = packetListMapper.lastestIdSize().size();//packetListService.getTotalRecords(request);//this.getTotalRecords(request,search);
            //总页数
            int totalPages = totalRecords / limit;
            if (totalRecords % limit != 0) {
                totalPages++;
            }
            List<PacketDTO> contents = packetListService.lastestList(start,limit);

            //当前显示页码
            int currPage = Math.min(totalPages, page);
            dateResponse.setTotalRecords(totalRecords);
            dateResponse.setTotalPages(totalPages);
            dateResponse.setPageNo(currPage);
            dateResponse.setContents(contents);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateResponse;
    }

    @RequestMapping("meterPacketList")
    public DataResponse<Packet> meterlist(HttpServletRequest request,
                                     @RequestParam(value="pageNo") Integer pageNo,
                                     @RequestParam(value="rows") Integer rows){
        DataResponse<Packet> dateResponse = new DataResponse();
        try {
            int page = pageNo<= 0 ? 1:pageNo;
            //每页显示数量
            int limit = rows< 0 ? 20 : rows;
            //从哪条记录开始查
            int start = limit * (page - 1);
            start = start < 0 ? 0 : start;
            //总记录数
            int totalRecords = packetListService.getMeterTotalRecords(request);//this.getTotalRecords(request,search);
            //总页数
            int totalPages = totalRecords / limit;
            if (totalRecords % limit != 0) {
                totalPages++;
            }
            List<Packet> contents = packetListService.meterlist(request,start,limit);

            //当前显示页码
            int currPage = Math.min(totalPages, page);
            dateResponse.setTotalRecords(totalRecords);
            dateResponse.setTotalPages(totalPages);
            dateResponse.setPageNo(currPage);
            dateResponse.setContents(contents);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateResponse;
    }

    /*累计能耗*/
    @RequestMapping("eneryTotal")
    public List<Energy> eneryTotal(@RequestParam("id") Integer id){
        return packetListService.eneryTotal(id);
    }
    @RequestMapping("checkAlarm")
    public List<PacketDTO> checkAlarm(){
        return packetListService.checkAlarm();
    }

    @RequestMapping("mappings")
    public List<Mapping> getMappings(){
        return packetListMapper.getMapping();
    }

}
